package 多线程;

import java.util.concurrent.Semaphore;

/**
 示例 1:
 输入: "HOH"
 输出: "HHO"
 解释: "HOH" 和 "OHH" 依然都是有效解。
 示例 2:
 输入: "OOHHHH"
 输出: "HHOHHO"
 解释: "HOHHHO", "OHHHHO", "HHOHOH", "HOHHOH", "OHHHOH", "HHOOHH", "HOHOHH" 和 "OHHOHH" 依然都是有效解。


 */

public class _1117H2O生成 {

    public _1117H2O生成() {

    }
     private Semaphore h = new Semaphore(2);
    private Semaphore  o = new Semaphore(0);

    public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
        h.acquire(1);
        // releaseHydrogen.run() outputs "H". Do not change or remove this line.
        releaseHydrogen.run();
        o.release(1);
    }

    public void oxygen(Runnable releaseOxygen) throws InterruptedException {
        o.acquire(2);
        // releaseOxygen.run() outputs "O". Do not change or remove this line.
        releaseOxygen.run();
        h.release(2);
    }
}
